package com.hjk.waibao.mj;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


public class PdfUtilImg {
    public static final Rectangle A4_HOR = new RectangleReadOnly(842.0F, 595.0F);

    public static File Pdf(List<String> imageUrllist, String mOutputPdfFileName, String url, String date) {
        Document doc = new Document(); //new一个pdf文档
        Rectangle pageSize = new Rectangle(A4_HOR);
        doc.setPageSize(pageSize);

        try {
            PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName)); //pdf写入
            doc.open();//打开文档
            Font headFont = FontFactory.getFont("FONT", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 9, Font.NORMAL, BaseColor.BLACK);
            //添加标题文本
            Phrase datePh = new Phrase(date, headFont);
            Phrase urlPh = new Phrase(url, headFont);
            float center = doc.getPageSize().getRight() / 2;//页面的水平中点
            float top = doc.getPageSize().getTop() - 55;
            float bottom = doc.getPageSize().getBottom() + 55;

            for (int i = 0; i < imageUrllist.size(); i++) {  //循环图片List，将图片加入到pdf中
                doc.newPage();  //在pdf创建一页
                Image png1 = Image.getInstance(imageUrllist.get(i)); //通过文件路径获取image
                float heigth = png1.getHeight();
                float width = png1.getWidth();
                int percent = getPercent2(heigth, width);
                png1.setAlignment(Image.MIDDLE);
                png1.scalePercent(percent);// 表示是原来图像的比例;
                Paragraph paragraph1 = new Paragraph("  ", headFont);
                paragraph1.setSpacingAfter(20);//距离后面行距
                doc.add(paragraph1);
                doc.add(png1);
                Phrase pageNumberPh = new Phrase((i + 1) + "/" + imageUrllist.size(), headFont);
                /** 参数xy是指文本显示的页面上的哪个店。alignment指文本在坐标点的对齐方式 */
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, urlPh, 40, top, 0);
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, datePh, center, top, 0);
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, datePh, doc.getPageSize().getRight() - 70, top, 0);
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, urlPh, 40, bottom, 0);
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, datePh, center, bottom, 0);
                ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, pageNumberPh, doc.getPageSize().getRight() - 70, bottom, 0);
            }
            doc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File mOutputPdfFile = new File(mOutputPdfFileName);  //输出流
        if (!mOutputPdfFile.exists()) {
            mOutputPdfFile.deleteOnExit();
            return null;
        }
        return mOutputPdfFile; //反回文件输出流
    }

    public static int getPercent(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
//        if (h < w) {
        p2 = 297 / w * 100;
//        } else {
//            p2 = 210 / h * 100;
//        }
        p = Math.round(p2);
        return p;
    }

    public static int getPercent2(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        p2 = 772 / w * 100;
        p = Math.round(p2);
        return p;
    }

}
